<script setup lang="ts">
import { Handle, Position, useVueFlow } from '@vue-flow/core'
import { computed,ref, watch,onMounted,nextTick } from 'vue'
import StyleSet from './StyleSet.vue';
const props = defineProps<{
  id: string;
  data: {
    name: string;
    ClearAll?: boolean;
  };
  toObject?:()=>any
}>()

const { updateNodeData,updateNode} = useVueFlow()

const name = computed({
  get: () => props.data.name,
  set: (name) => updateNodeData(props.id, {...props.data, name: name }),
})

import useConnectionValid from './ts/connectionValid';
const { isValidConnection } = useConnectionValid();

// 节点属性
const ClearAll = ref(props.data.ClearAll);
watch(() => [ClearAll.value], (newValue) => {
  updateNodeData(props.id, {...props.data, ClearAll: ClearAll.value })
});
const AgentContentConainer = ref<HTMLElement | null>(null);
let observer: ResizeObserver;
import { useProjectsStore } from '@/stores/projectsStore';
import type { Styles } from '@vue-flow/core';
let projectStore = useProjectsStore();
onMounted(() => {
  // console.log("props:", props)
    let now_store_node = projectStore.getNode(props.id);
    let STY = <Styles>now_store_node?.style || {};
    if (now_store_node) {
      WW.value = STY?.minWidth ? parseInt(STY.minWidth.toString()) : MaxWW;
      HH.value = STY?.minHeight ? parseInt(STY.minHeight.toString()) : MaxHH;
      Color.value = STY?.backgroundColor || ClearBackground;
    }
    if (!AgentContentConainer.value) return;
    updateSize();
    const { width, height } = AgentContentConainer.value!.getBoundingClientRect();
    //console.log('AgentContentConainer:', width, height);

});
import { ClearMinW, ClearMinH, ClearBackground } from './ts/default-node-style';
const MaxWW=ClearMinW;
const MaxHH=ClearMinH;
const WW = ref(MaxWW);
const HH = ref(MaxHH);
const Color= ref(ClearBackground);
const updateColor=(color:string)=>{
  Color.value = color;
  updateSize()
}
const onChangeSize = () => {
  updateSize()
}

const updateSize=()=>{
  WW.value = Math.max(WW.value, MaxWW);
  HH.value = Math.max(HH.value, MaxHH);
  updateNode(props.id, {...props, style:{minWidth: `${WW.value}px`,minHeight: `${HH.value}px`,backgroundColor: Color.value}})
  //console.log('更新样式:',props.toObject?.())
}
</script>

<template>
    <div class="AgentName"> <input :id="`${id}-input`" v-model="name"  class="nodrag"> </div>
    <div class="AgentContent" ref="AgentContentConainer">
      <Handle id="flow-in" type="target" :class="['AgentFlowInputHandle','top-0']" :position="Position.Left" title="FLow In"  :connectable="()=>{return isValidConnection(props.id,'flow-in', 'target')}" ><span class="HandleLeftSpan">Flow In</span></Handle>
      
      <div class="NodeData">
        <!-- checkbox  -->
        <div class="input-row">
          <label for="ClearAll">Clear All</label>
          <input
            id="ClearAll"
            type="checkbox"
            v-model="ClearAll"
            :class="['checkbox','nodrag']"
          />
        </div>

      </div>
    </div>
  <div class="setfooter2" style="height:30px;width:200px;">
    W:<span class="input2"> <input :id="`${id}-input1`" v-model="WW" class="nodrag" @change="onChangeSize" min="200" max="1000"> </span>
    H:<span class="input2"> <input :id="`${id}-input2`" v-model="HH" class="nodrag" @change="onChangeSize" min="100" max="500"> </span>
  </div>
  <div class="setfooter" style="height:30px;width:70px;">
    <StyleSet @update_style_color="updateColor"></StyleSet>
  </div>
</template>


<style scoped>
.setfooter{
position: absolute;
right:0;
bottom:3px;
}
.setfooter2{
position: absolute;
left:3px;
bottom:0px;
}
.input2 {
  position: relative;
  display: inline-block;
  font-size: 14px;
  text-align: center;
}
.input2 input {
  max-height: 20px;
  height:30px;
  width: 50px;
  border: none;
  outline: none;
  background: transparent;
  color: #333;
  font-size: 16px;
  padding: 8px 12px;
  transition: background-color 0.3s ease, box-shadow 0.3s ease;
  text-align: center;
  background-color: #00000000;
}
.AgentName {
  position: relative;
  display: inline-block;
  font-size: 14px;
  width: 100%;
  padding-left: 70px;
  padding-right: 70px;
  text-align: center;
}

.AgentName input {
  max-height: 20px;
  height: 100%;
  width: 100%;
  border: none;
  outline: none;
  background: transparent;
  color: #333;
  font-size: 16px;
  padding: 8px 12px;
  transition: background-color 0.3s ease, box-shadow 0.3s ease;
  text-align: center;
  background-color: #00000000;
}

/* 输入框聚焦时 */
.AgentName input:focus {
  background-color: #f5f5f5;
  box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.5);
  color: #000;
}

/* 输入框没有内容时（placeholder显示） */
.AgentName input:placeholder-shown {
  color: #ffffff;
}

/* 输入框内容区域边距 */
.AgentName input::placeholder {
  color: #ccc;
  font-style: italic;
}

/* 输入框平滑过渡效果 */
.AgentName input {
  transition: background-color 0.3s, box-shadow 0.3s, color 0.3s;
}
.AgentContent{
  height: 100%;
  width: 100%;
}
</style>